package SpaceIT.pages;

import java.util.HashMap;

import model.dao.PaypalDAO;
import model.pojo.Cart;
import model.pojo.Paypal;
import model.util.paypalfunctions;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.apache.tapestry5.services.Session;


public class expresscheckout {
	@Inject
	private Request request;
	@Inject
	private Response response;
	
	@ApplicationState
	private Cart cart;
	
	public void onActivate()
	{
		 try
		 {
			 
			Paypal pp = PaypalDAO.LayThongTinPaypal();
			
		
			Session session = request.getSession(true);
	        /*
	        '-------------------------------------------
	        ' The paymentAmount is the total value of
	        ' the shopping cart, that was set
	        ' earlier in a session variable
	        ' by the shopping cart page
	        '-------------------------------------------
	        */
			//chia tu tien viet sang tien USD
			
			int tienTT = cart.TongTien()/(int)pp.getTyGia();
			//System.out.println("tien  "+tienTT);
	        session.setAttribute("Payment_Amount",String.valueOf(tienTT));
	        String paymentAmount = (String) session.getAttribute("Payment_Amount");
	
	
	        /*
	        '------------------------------------
	        ' The returnURL is the location where buyers return to when a
	        ' payment has been succesfully authorized.
	        '
	        ' This is set to the value entered on the Integration Assistant
	        '------------------------------------
	        */
	
	        String returnURL = "http://localhost:8080/SpaceIT/review";
	
	        /*
	        '------------------------------------
	        ' The cancelURL is the location buyers are sent to when they hit the
	        ' cancel button during authorization of payment during the PayPal flow
	        '
	        ' This is set to the value entered on the Integration Assistant
	        '------------------------------------
	        */
	        String cancelURL = "http://localhost:8080/SpaceIT/error";
	
	        /*
	        '------------------------------------
	        ' Calls the SetExpressCheckout API call
	        '
	        ' The CallShortcutExpressCheckout function is defined in the file PayPalFunctions.asp,
	        ' it is included at the top of this file.
	        '-------------------------------------------------
	        */
	        //paypalfunctions ppf = new paypalfunctions();
	        paypalfunctions ppf = new paypalfunctions(pp);
	       // System.out.println("oây "+ppf.toString());
	        HashMap nvp = ppf.CallShortcutExpressCheckout (paymentAmount, returnURL, cancelURL);
	       // System.out.println("sau  "+nvp.get("ACK").toString());
	        String strAck = nvp.get("ACK").toString();
	        if(strAck !=null && strAck.equalsIgnoreCase("Success"))
	        {
	        	//System.out.println("trong  "+ppf.toString());
	            session.setAttribute("TOKEN", nvp.get("TOKEN").toString());
	            //' Redirect to paypal.com
	            //response.sendRedirect(response.encodeRedirectURL( nvp.get("TOKEN").toString() ));
	            ppf.RedirectURL( response,nvp.get("TOKEN").toString() );
	        }
	        else
	        {
	            // Display a user friendly Error on the page using any of the following error information returned by PayPal
	
	            String ErrorCode = nvp.get("L_ERRORCODE0").toString();
	            String ErrorShortMsg = nvp.get("L_SHORTMESSAGE0").toString();
	            String ErrorLongMsg = nvp.get("L_LONGMESSAGE0").toString();
	            String ErrorSeverityCode = nvp.get("L_SEVERITYCODE0").toString();
	        }
		 }
		 catch (Exception e) {
			// TODO: handle exception
			 System.out.println("loi het"+e.getMessage());
		}
		
	}	
	
	
}
